<?php
class ModelUserUser extends Model {
	public function addUser($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "user` SET username = '" . $this->db->escape($data['username']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) ."', company_name = '" . $this->db->escape($data['company_name']) 
		. "', opf = '" . $this->db->escape($data['opf']). "', okpo = '" . $this->db->escape($data['okpo']) . "', okonx = '" . $this->db->escape($data['okonx']) . "', inn = '" . $this->db->escape($data['inn']) . "', user_group_id = '" . (int)$data['user_group_id'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
		
		$user_id = $this->db->getLastId();
		
		$this->db->query("INSERT INTO `" . DB_PREFIX . "address` SET certification = '" . $this->db->escape($data['image']) . "',licenses = '" . $this->db->escape($data['image_l']) . "', user_id = '" . (int)$user_id . "'");
		
		$address_id = $this->db->getLastId();

      	$this->db->query("UPDATE " . DB_PREFIX . "user SET address_id = '" . (int)$address_id . "' WHERE user_id = '" . (int)$user_id . "'");	
		
	}
	
	public function editUser($user_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "user` SET username = '" . $this->db->escape($data['username']) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', company_name = '" . $this->db->escape($data['company_name']) . "' , opf = '" . $this->db->escape($data['opf']) . "' , okpo = '" . $this->db->escape($data['okpo']) . "' , okonx = '" . $this->db->escape($data['okonx']) . "' , inn = '" . $this->db->escape($data['inn']) . "', user_group_id = '" . (int)$data['user_group_id'] . "', status = '" . (int)$data['status'] . "', contract = '" . (int)$data['contract'] . "' WHERE user_id = '" . (int)$user_id . "'");
		
		if ($data['password']) {
			$this->db->query("UPDATE `" . DB_PREFIX . "user` SET password = '" . $this->db->escape(md5($data['password'])) . "' WHERE user_id = '" . (int)$user_id . "'");
		}
		if (isset($data['image'])) {
			$this->db->query("UPDATE `" . DB_PREFIX . "address` SET certification = '" . $this->db->escape($data['image']) . "' WHERE user_id = '" . (int)$user_id . "'");
		}
		if (isset($data['image_l'])) {
			$this->db->query("UPDATE `" . DB_PREFIX . "address` SET licenses = '" . $this->db->escape($data['image_l']) . "' WHERE user_id = '" . (int)$user_id . "'");
		}
	}
	
	public function deleteUser($user_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "user` WHERE user_id = '" . (int)$user_id . "'");
		$this->db->query("DELETE FROM `" . DB_PREFIX . "address` WHERE user_id = '" . (int)$user_id . "'");
	}
	
	public function getUser($user_id) {
		$query = $this->db->query("SELECT u.*,a.certification AS image, a.licenses AS image_l  FROM `" . DB_PREFIX . "user` u LEFT JOIN `" . DB_PREFIX . "address` a on a.user_id = u.user_id  WHERE u.user_id = '" . (int)$user_id . "'");	
		return $query->row;
	}
	
	public function getUserCertification($user_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE user_id = '" . (int)$user_id . "'");
	
		return $query->row;
	}
	
	public function getUsers($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "user`";
			
		$sort_data = array(
			'username',
			'status',
			'date_added'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY username";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	public function getTotalUsers() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "user`");
		
		return $query->row['total'];
	}

	public function getTotalUsersByGroupId($user_group_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "user` WHERE user_group_id = '" . (int)$user_group_id . "'");
		
		return $query->row['total'];
	}
}
?>